package com.etc.action;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.regex.Pattern;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;

import com.etc.bean.User;
import com.etc.service.UserService;
import com.opensymphony.xwork2.ActionSupport;
import com.opensymphony.xwork2.ModelDriven;
import com.opensymphony.xwork2.Preparable;

@Controller
public class UserAction extends ActionSupport implements ModelDriven<User>,
		Preparable {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	@Autowired
	private UserService service;
	private String repassword;
	private String name;
	

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	private User user;

	public User getModel() {
		return user;
	}

	public void prepare() throws Exception {
		user = new User();
	}

	@Override
	public String execute() throws Exception {
		Date currentTime = new Date();
		SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
		String dateString = formatter.format(currentTime);
		System.out.println(dateString+"--------------");
		user.setRegtime(dateString);
		service.Regist(user);
		return super.execute();
	}

	public void setRepassword(String repassword) {
		this.repassword = repassword;
	}

	public String getRepassword() {
		return repassword;
	}

	public String login(){
		
		boolean b = Pattern.matches("^[a-zA-Z0-9]{1}([._a-zA-Z0-9-]+)(.[_a-zA-Z0-9-]+)*@[a-z0-9-]+(.[a-z0-9-]+){1,3}$",name );
		User user1=null;
		if (b) {
			user.setEmail(name);
		}else {
			user.setUsername(name);
		}
		user1=service.Login(user);
		if(null!=user1){
			return SUCCESS;
		}
		return "fail";
		
		
	}
	
}
